{#

This file is part of the Sonata package.

(c) Thomas Rabaix <thomas.rabaix@sonata-project.org>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

#}

{% extends base_template %}

{% block actions %}
    <div class="sonata-actions">
        <ul>
            <li class="sonata-action-element"><a href="{{ admin.generateUrl('create') }}">{% trans "link_action_create" from admin.translationdomain %}</a></li>
        </ul>
    </div>
{% endblock %}

{% block side_menu %}{% if side_menu %}{{ side_menu.render|raw }}{% endif %}{% endblock %}

{% block list_table %}
    {% if datagrid.results|length > 0 %}
        <form action="{{ admin.generateUrl('batch') }}" method="POST" >
            <table>
                {% block table_header %}
                    <tr>
                        {% for field_description in list.elements %}
                            {% if field_description.getOption('code') == '_batch' %}
                                <th class="sonata-ba-list-field-header sonata-ba-list-field-header-batch">
                                  <input type="checkbox" id="list_batch_checkbox" />
                                </th>
                            {% else %}
                                    {% spaceless %}<th class="sonata-ba-list-field-header sonata-ba-list-field-header-{{ field_description.type}}">
                                        {% if field_description.options.name is defined %}
                                            {% trans field_description.options.name from admin.translationdomain %}
                                        {% else %}
                                            {% trans field_description.name from admin.translationdomain %}
                                        {% endif %}
                                    </th>{% endspaceless %}
                            {% endif %}
                        {% endfor %}
                    </tr>
                {% endblock %}

                {% block table_body %}
                    {% for object in datagrid.results %}
                        <tr>
                            {% for field_description in list.elements %}
                                {{ object|render_list_element(field_description) }}
                            {% endfor %}
                        </tr>
                    {% endfor %}
                {% endblock %}

                {% block table_footer %}
                    {% if datagrid.pager.haveToPaginate() %}
                        <tr>
                            <td colspan="{{ list.elements|length }}" class="pager">
                                <ul>
                                    {% if datagrid.pager.page != datagrid.pager.previouspage %}
                                        <li><a href="{{ admin.generateUrl('list', datagrid.values|merge({'page': datagrid.pager.previouspage})) }}">{% trans 'link_previous_pager' from 'AdminBundle' %}</a></li>
                                    {% endif %}

                                    {# Set the number of pages to display in the pager #}
                                    {% for page in datagrid.pager.getLinks(5) %}
                                        {% if page == datagrid.pager.page %}
                                            <li>{{ page }}</li>
                                        {% else %}
                                            <li><a href="{{ admin.generateUrl('list', datagrid.values|merge({'page': page})) }}">{{ page }}</a></li>
                                        {% endif %}
                                    {% endfor %}

                                    {% if datagrid.pager.page != datagrid.pager.nextpage %}
                                        <li><a href="{{ admin.generateUrl('list', datagrid.values|merge({'page': datagrid.pager.nextpage})) }}">{% trans 'link_next_pager' from 'AdminBundle' %}</a></li>
                                    {% endif %}
                                </ul>
                            </td>
                        </tr>
                    {% endif %}
                {% endblock %}
            </table>
        
            <script type="text/javascript">
                jQuery(document).ready(function($){
                   $('#list_batch_checkbox').click(function(){
                       $(this).closest('table').find("td input[type='checkbox']").attr('checked', $(this).is(':checked'));
                   });
                });
            </script>

            <div class="sonata-ba-list-actions">
                <select name="action">
                    {% for action, label in admin.batchactions %}
                        <option value="{{ action }}">{{ label }}</option>
                    {% endfor %}
                </select>
                <input type="submit" value="{% trans 'btn_batch' from 'AdminBundle' %}" />
            </div>
        </form>
    {% else %}
        <p class="notice">
          {% trans 'no_result' from 'AdminBundle' %}
        </p>    
    {% endif %}
{% endblock %}

{% block list_filters %}
    {% if datagrid.filters %}
        <form action="{{ admin.generateUrl('list') }}" method="GET">
            {% for filter in datagrid.filters %}
                <div>
                    {{ filter|render_filter_element }}</td>
                </div>
            {% endfor %}

            <input type="submit" value="{% trans 'btn_filter' from 'AdminBundle' %}" />

            <a href="{{ admin.generateUrl('list') }}">{% trans 'link_reset_filter' from 'AdminBundle' %}</a>
        </form>
    {% endif %}
{% endblock %}

